package lanou.giftsay_01.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import lanou.giftsay_01.R;
import lanou.giftsay_01.tools.NewBean;

/**
 * Created by dllo on 16/8/30.
 */
public class MineRvAdapter extends RecyclerView.Adapter<MineRvAdapter.MrvViewHolder> {
    private Context context ;
    private ArrayList<NewBean> collectionBeen ;

    public MineRvAdapter(Context context) {
        this.context = context;
    }

    public void setNewBeen(ArrayList<NewBean> collectionBeen) {
        this.collectionBeen = collectionBeen;
        notifyDataSetChanged();
    }

    @Override
    public MrvViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_hot , parent ,false);
        MrvViewHolder mrvViewHolder = new MrvViewHolder(view);
        return mrvViewHolder;
    }

    @Override
    public void onBindViewHolder(MrvViewHolder holder, int position) {
        ArrayList<NewBean> newBeen = new ArrayList<>();
        for (int i = 0; i <collectionBeen.size() ; i++) {
            newBeen.add(collectionBeen.get(i));
        }
        Collections.sort(newBeen ,new PxBean());
      holder.tv.setText(newBeen.get(position).getName());
      holder.title.setText(newBeen.get(position).getPrice());
      holder.like.setText(newBeen.get(position).getFavorites_count());
        Picasso.with(context).load(newBeen.get(position).getCover_image_url()).into(holder.iv);
    }

    @Override
    public int getItemCount() {
        return collectionBeen != null ? collectionBeen.size() : 0;
    }

    public class MrvViewHolder extends RecyclerView.ViewHolder {
        TextView tv ;
        TextView title ;
        TextView like ;
        ImageView iv ;
        public MrvViewHolder(View itemView) {
            super(itemView);
            tv = (TextView) itemView.findViewById(R.id.hot_tv);
            title = (TextView) itemView.findViewById(R.id.hot_title);
            like = (TextView) itemView.findViewById(R.id.hot_like_count);
            iv = (ImageView) itemView.findViewById(R.id.hot_iv);
        }
    }
    class PxBean implements Comparator {

        @Override
        public int compare(Object lhs, Object rhs) {
            NewBean s1 = (NewBean) lhs;
            NewBean s2 = (NewBean) rhs;
            return new Long(s2.getTime()).compareTo(new Long(s1.getTime()));
        }
    }
}
